package com.novabluetooth.services.servicedevices;

import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattDescriptor;
import android.bluetooth.BluetoothGattService;
import android.util.Log;
import com.novabluetooth.services.TJBluetoothLeService;

import java.util.List;
import java.util.UUID;

/**
 *体重 脂肪秤
 * 
 * @author nova
 *
 */
public class TiZhongFatScaleService extends TJBluetoothLeService {

	String CHARACTERISTIC_UUID_RETURN="00002902-0000-1000-8000-00805f9b34fb";


	//fatScale
	String DEVICE_NAME_FAT  = "FatScale";
	String DEVICE_NAME_FAT1  = "000FatScale01";
	String DEVICE_NAME_FAT2  = "000FatScale02";
	String DEVICE_NAME_FAT3  = "Holtek_ BodyFatScal";
	String DEVICE_NAME_FAT4 = "Health Scale";
//	String DEVICE_NAME_FAT4 = "HeartRate Scale";
	String UUID_REDFAT = "0000fff1-0000-1000-8000-00805f9b34fb";
	String UUID_WRITEFAT =  "0000fff2-0000-1000-8000-00805f9b34fb";
	//fat
	String WRITE="0000fff6-0000-1000-8000-00805f9b34fb";
	String WRITE_FAT="0000fff2-0000-1000-8000-00805f9b34fb";

	//model 1
	String SERVICE_UUID_01 = "00001910-0000-1000-8000-00805f9b34fb";
	String CH_01 = "0000fff2-0000-1000-8000-00805f9b34fb";
	//model2
	String SERVICE_UUID_02="0000fff0-0000-1000-8000-00805f9b34fb";
	String UUID_CH_SERVICE_NOTIFY="0000fff6-0000-1000-8000-00805f9b34fb";

	//ear
	String DEVICE_NAME_EAR  = "FSRKB-EWQ01";
	String CH_UUID_NOTIFY_EAR  = "0000fff2-0000-1000-8000-00805f9b34fb";


	//TURGOSCOPE
	String TURGOSCOPE_WRITE_CHARA = "0000fff6-0000-1000-8000-00805f9b34fb";
	String TURGOSCOPE_NAME1 = "FSRKB_BT-001";
	String TURGOSCOPE_NAME2 = "FSRKB_BT-002";
	String TURGOSCOPE_NAME = "FSRKB_BT";

	String GLUCOMETER = "Glucometer";


	BluetoothGattService bleGattService = null;

	//记录上次的值
	String strLastData = "";
	@Override
	public void servicesDiscovered(int status) {
		super.servicesDiscovered(status);

		Log.e( TAG,"------onServicesDiscovered----");
		// model=1 1910 model=2 fff0
		int model = 0;
		List<BluetoothGattService> services = mBluetoothGatt.getServices();
		for(int i = 0 ; i<services.size();i++){
			String serviceUUID2 = services.get(i).getUuid().toString();
			Log.d("serviceUUIDdddd---",serviceUUID2);
		}
		for (int i = 0; i < services.size(); i++) {
			String serviceUUID = services.get(i).getUuid().toString();
			Log.e( TAG,"----serviceUUID----" + serviceUUID);
			if(serviceUUID.equals(SERVICE_UUID_01)){
				model = 1;
				Log.e( TAG,"------model 1------");
				break;
			}else if(serviceUUID.equals(SERVICE_UUID_02)){
				model = 2;
				Log.e( TAG,"------model 2------");
				break;
			}
		}

		if((model==1)||deviceName.equals(DEVICE_NAME_EAR)){
			Log.e(TAG, "-----ear-/-model 01---");
			bleGattService = mBluetoothGatt.getService(UUID.fromString(SERVICE_UUID_01));
			//only read
			BluetoothGattCharacteristic characteristicRead = bleGattService
					.getCharacteristic(UUID.fromString(CH_01));
			 setCharacterValue(characteristicRead,null, status);
		}else if((model==2)){
			if(deviceName.equals(DEVICE_NAME_FAT3)){//DEVICE_NAME_FAT4 ||deviceName.equals(DEVICE_NAME_FAT4)
				Log.e( TAG,"-----Holtek_BodyFatScal------");
				bleGattService = mBluetoothGatt
						.getService(UUID.fromString(SERVICE_UUID_02));
				//write
				writeGattCharact = bleGattService
						.getCharacteristic(UUID.fromString(UUID_WRITEFAT));
				//read
				readGattCharact = bleGattService
						.getCharacteristic(UUID.fromString(UUID_REDFAT));
				//notify
				BluetoothGattCharacteristic	characteristicNotify = bleGattService
						.getCharacteristic(UUID.fromString(UUID_CH_SERVICE_NOTIFY));
				setCharacterValue(readGattCharact, characteristicNotify, status);
			}else if(deviceName.equals(DEVICE_NAME_FAT1)||
					deviceName.equals(DEVICE_NAME_FAT2)||
					deviceName.contains(DEVICE_NAME_FAT)){
				Log.e( TAG, "-----fatScale------");
				bleGattService = mBluetoothGatt
						.getService(UUID.fromString(SERVICE_UUID_02));
				//read
				readGattCharact = bleGattService.getCharacteristic(UUID.fromString(UUID_REDFAT));
				//notify
				BluetoothGattCharacteristic	characteristicNotify = bleGattService
						.getCharacteristic(UUID.fromString(UUID_CH_SERVICE_NOTIFY));
				setCharacterValue(readGattCharact, characteristicNotify , status);
			}else if(deviceName.equals(DEVICE_NAME_FAT4)){
                Log.e( TAG,"-------genenral model---read---");
                bleGattService = mBluetoothGatt
                        .getService(UUID.fromString(SERVICE_UUID_02));
                //only read
                readGattCharact = bleGattService.getCharacteristic(UUID.fromString(UUID_REDFAT));
                setCharacterValue(readGattCharact,null, status);
                Log.e("-------走-------",status+"");
            }else{
				Log.e( TAG,"-------genenral model------");
				bleGattService = mBluetoothGatt
						.getService(UUID.fromString(SERVICE_UUID_02));
				//only read
				readGattCharact = bleGattService.getCharacteristic(UUID.fromString(TURGOSCOPE_WRITE_CHARA));
				setCharacterValue(readGattCharact,null, status);
			}
		}

	}

	@Override
	public boolean isRightData(byte[] buffer) {
		super.isRightData(buffer);


		// 体温
		int readSize = buffer.length;
		if (readSize <= 0) {
			Log.e( TAG , "readSize《=0");
			ifSuccess = false;
		} else {

			//数据02D80243000000000000000099AA   开始测量 02D8  【默认单位 是Kg， 如果需要其他单位切换，请看蓝牙文档】
			//数据02DD024300000000000010800CAA   测量结果 02DD
			//数据020F0F0F0F0000000000000000AA   测量结束 020F0F0F0F
			String getData = byteArray2HexString(buffer, readSize);

			Log.e("TAG","----data----" + getData);
			if (getData.startsWith("02D8")){
				Log.e("TAG","脂肪秤刚开始测量");
			} else if (getData.startsWith("02DD")){
				Log.e("TAG","脂肪秤测量结果");
				strLastData = getData;
			} else if (getData.startsWith("020F0F0F0F")){
				Log.e("TAG","脂肪秤测量结束");
				if (strLastData.length() == 28){
					int tf = Integer.parseInt(strLastData.substring(4, 8), 16);
					float bg = (float) (tf / 10.0);
					checkData = String.valueOf(bg);
					Log.e("解析结果：---",checkData);
					ifSuccess = true;
				}
			}
		}
		return ifSuccess;

	}

	@Override
	public void closeDevice() {
		super.closeDevice();
	}


	private void setCharacterValue(BluetoothGattCharacteristic characteristic,
								   BluetoothGattCharacteristic characteristic1,int status){
		// 激活通知
		final int charaProp = characteristic.getProperties();
		int charaProp_second = -1;
		if (characteristic1!=null) {
			charaProp_second = characteristic1.getProperties();
		}
		if (status == BluetoothGatt.GATT_SUCCESS) {
			if ((charaProp & BluetoothGattCharacteristic.PROPERTY_NOTIFY) >0) {
				Log.e( TAG,"--------具有通知属性----charaProp-" + charaProp);
				mBluetoothGatt.setCharacteristicNotification(
						characteristic, true);
				BluetoothGattDescriptor descriptor = characteristic
						.getDescriptor(UUID
								.fromString(CHARACTERISTIC_UUID_RETURN));
				if (descriptor != null) {
					descriptor
							.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
					Log.e( TAG, characteristic.getUuid() +
							"激活通知属性： " + mBluetoothGatt.writeDescriptor(descriptor));
					if (characteristic1==null) {
//						sendBroadCast(Constant.ACTION_GATT_CONNECTED,
//								Constant.ACTION_GATT_CONNECTED, getString(R.string.connectedSuccessful));// 激活通知属性发送广播
					}
				}
				if(characteristic1==null){
					return;
				}
				if ((charaProp_second & BluetoothGattCharacteristic.PROPERTY_NOTIFY) >0) {
					Log.e( TAG,"--------具有通知属性-------");
					mBluetoothGatt.setCharacteristicNotification(
							characteristic1, true);
					if (descriptor != null) {
						descriptor
								.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
						Log.e( TAG,characteristic1.getUuid() + "激活通知属性： "
								+ mBluetoothGatt.writeDescriptor(descriptor));
						Log.e( TAG,"----------体重称连接成功---------------");
//						sendBroadCast(Constant.ACTION_GATT_CONNECTED,
//								Constant.ACTION_GATT_CONNECTED, getString(R.string.connectedSuccessful));// 激活通知属性发送广播
					}
				}
			}
		}
	}

}
